<?php

namespace App\Http\Controllers\Mgt;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Repositories\Eloquent\Mgt\UserRepository as User;
use App\Repositories\Eloquent\Mgt\BookRepository as Book;
use App\Repositories\Eloquent\Mgt\ActivityRepository as Activity;
use App\Repositories\Eloquent\Biz\BorrowRepository as Borrow;

class VolunteerController extends Controller
{
    protected $volunteer, $book, $activity, $borrow;

    public function __construct(User $volunteer, Book $book, Activity $activity, Borrow $borrow)
    {
        parent::__construct();

        $this->volunteer = $volunteer;
        $this->book = $book;
        $this->activity = $activity;
        $this->borrow = $borrow;
    }

    public function sign(Request $request)
    {
        $volunteers = $books = $borrows = array();

        $activity = \App\Models\Mgt\Activity::current();

        $query = $request->input('query');
        $account = $request->input('account');
        $isbn = $request->input('isbn');

        if (isset($account))
            $volunteers = $this->volunteer->index($account);


        if (isset($query)) {

            if (preg_match('/^\d+$/i', $query) && strlen($query) == 5) {

                $account = $query;

                $volunteers = $this->volunteer->index($account);
            }

            if (preg_match('/^\d+$/i', $query) && strlen($query) == 13) {

                $isbn = $query;

                $map = $this->book->index($isbn)->pluck('id');

                if (!empty($map))
                    $borrows = $this->borrow->wheres($map)->get();

                $books = $this->book->index($query);
            }

        }

        return view($this->view, compact('activity', 'users', 'account', 'books', 'borrows', 'isbn'));
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = $request->query('query');

        $volunteers = $this->volunteer->index($query, \App\Models\Mgt\User::USER_TYPE_VOLUNTEER)->paginate(60);

        $volunteers = $this->volunteer->transforms($this->act, $volunteers);      

        $paging = round(count($volunteers)/2);

        return view($this->view, [
                        'users'=>$volunteers, 
                        'activity'=>$this->act, 
                        'paging'=>$paging,
                    ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view($this->view);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->volunteer->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = $this->volunteer->show($id);

        if ($user instanceof JsonResponse && $user->getStatusCode() === 404) {
            return abort('404');
        }

        $signs = $user->tracks;

        $borrows = $user->borrows;

        $returns = $user->returns;

        $points = $user->records;

        return view($this->view, compact('user', 'signs', 'borrows', 'returns', 'points'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = $this->volunteer->show($id);

        if ($user instanceof JsonResponse && $user->getStatusCode() === 404) {
            return abort('404');
        }

        $user = $this->volunteer->transform($this->act, $user);

        return view($this->view, compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->volunteer->renew($request, $id);
    }


    public function view($id)
    {
        $user = $this->volunteer->view($id);

        return view($this->view, compact('user'));
    }


    public function statistics(Request $request)
    {
        $rows = $this->volunteer->statistics($request);

        return view($this->view, compact('rows'));
    }

}
